package com.xinfu.newproject.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.ui.decorate.BackHandledInterface;
import com.xinfu.newproject.ui.decorate.CustomerInfoFragment;
import com.xinfu.newproject.ui.decorate.DecorateAuditFragment;
import com.xinfu.newproject.ui.decorate.DecorateCompanyFragment;
import com.xinfu.newproject.ui.decorate.DecoratePlanFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 报事报修
 */
public class DecorateActivity extends BaseActivity  implements BackHandledInterface {

    @BindView(R.id.layout_progress_tv1)
    TextView oneTv1;

    @BindView(R.id.layout_progress_tv2)
    TextView oneTv2;

    @BindView(R.id.layout_progress_tv3)
    TextView oneTv3;

    @BindView(R.id.layout_progress_tv4)
    TextView oneTv4;

    @BindView(R.id.layout_progress_two_tv1)
    TextView twoTv1;

    @BindView(R.id.layout_progress_two_tv2)
    TextView twoTv2;

    @BindView(R.id.layout_progress_two_tv3)
    TextView twoTv3;

    @BindView(R.id.layout_progress_two_tv4)
    TextView twoTv4;

    @BindView(R.id.layout_progress_v1)
    View v1;

    @BindView(R.id.layout_progress_v2)
    View v2;

    @BindView(R.id.layout_progress_two_v2)
    View twoV2;

    @BindView(R.id.layout_progress_v3)
    View v3;

    @BindView(R.id.layout_progress_two_v3)
    View twoV3;

    @BindView(R.id.layout_progress_v4)
    View v4;

    private CustomerInfoFragment customerInfoFragment;
    private DecorateCompanyFragment decorateCompanyFragment;
    private DecoratePlanFragment decoratePlanFragment;
    private DecorateAuditFragment decorateAuditFragment;

    private FragmentManager fragmentManager;
    public final static int FRAGMENT_ONE = 1;
    public final static int FRAGMENT_TWO = 2;
    public final static int FRAGMENT_THREE = 3;
    public final static int FRAGMENT_FOUR = 4;

    @BindView(R.id.head_text)
    TextView titleTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decorate);
        ButterKnife.bind(this);
        registerBack();
        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    private void initViewBind() {
        titleTv.setText("装修申请");
        fragmentManager = getSupportFragmentManager();
        showFragment(FRAGMENT_ONE);
    }

    public void showFragment(int index) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragments(ft);
        //注意这里设置位置
//        position = index;
        switch (index) {
            case FRAGMENT_ONE:
                /**
                 * 如果Fragment为空，就新建一个实例
                 * 如果不为空，就将它从栈中显示出来
                 */
                if (customerInfoFragment == null) {
                    customerInfoFragment = new CustomerInfoFragment();
                    ft.add(R.id.activity_decorate_fl, customerInfoFragment);

                } else {
                    ft.show(customerInfoFragment);
                }
                break;
            case FRAGMENT_TWO:
                if (decorateCompanyFragment == null) {
                    decorateCompanyFragment = new DecorateCompanyFragment();
                    ft.add(R.id.activity_decorate_fl, decorateCompanyFragment);
                } else {
                    ft.show(decorateCompanyFragment);
                }
                break;
            case FRAGMENT_THREE:
                if (decoratePlanFragment == null) {
                    decoratePlanFragment = new DecoratePlanFragment();
                    ft.add(R.id.activity_decorate_fl, decoratePlanFragment);
                } else {
                    ft.show(decoratePlanFragment);
                }
                break;
            case FRAGMENT_FOUR:
                if (decorateAuditFragment == null) {
                    decorateAuditFragment = new DecorateAuditFragment();
                    ft.add(R.id.activity_decorate_fl, decorateAuditFragment);
                } else {
                    ft.show(decorateAuditFragment);
                }
                break;
        }
        ft.commitAllowingStateLoss();
    }

    @Override
    public void setSelectedFragment(int selectedFragment) {
        switch (selectedFragment) {
            case FRAGMENT_TWO:
                oneTv1.setText("√");
                oneTv2.setTextColor(getResources().getColor(R.color.colorWhite));
                oneTv2.setBackgroundResource(R.drawable.custom_text_press);
                twoTv2.setTextColor(getResources().getColor(R.color.location_color));
                v1.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                v2.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                break;
            case FRAGMENT_THREE:
                oneTv1.setText("√");
                oneTv2.setText("√");
                oneTv2.setTextColor(getResources().getColor(R.color.colorWhite));
                oneTv3.setTextColor(getResources().getColor(R.color.colorWhite));
                oneTv2.setBackgroundResource(R.drawable.custom_text_press);
                oneTv3.setBackgroundResource(R.drawable.custom_text_press);
                twoTv2.setTextColor(getResources().getColor(R.color.location_color));
                twoTv3.setTextColor(getResources().getColor(R.color.location_color));
                v1.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                v2.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                v3.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                twoV2.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                break;
            case FRAGMENT_FOUR:
                oneTv1.setText("√");
                oneTv2.setText("√");
                oneTv3.setText("√");
                oneTv2.setTextColor(getResources().getColor(R.color.colorWhite));
                oneTv3.setTextColor(getResources().getColor(R.color.colorWhite));
                oneTv4.setTextColor(getResources().getColor(R.color.colorWhite));
                oneTv2.setBackgroundResource(R.drawable.custom_text_press);
                oneTv3.setBackgroundResource(R.drawable.custom_text_press);
                oneTv4.setBackgroundResource(R.drawable.custom_text_press);
                twoTv2.setTextColor(getResources().getColor(R.color.location_color));
                twoTv3.setTextColor(getResources().getColor(R.color.location_color));
                twoTv4.setTextColor(getResources().getColor(R.color.location_color));
                v1.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                v2.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                v3.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                v4.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                twoV2.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                twoV3.setBackgroundColor(getResources().getColor(R.color.yellow_ff));
                break;
        }
        showFragment(selectedFragment);
    }

    /**
     * 隐藏fragment
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (customerInfoFragment != null) {
            transaction.hide(customerInfoFragment);
        }
        if (decorateCompanyFragment != null){
            transaction.hide(decorateCompanyFragment);
        }
        if (decoratePlanFragment != null) {
            transaction.hide(decoratePlanFragment);
        }
        if (decorateAuditFragment != null) {
            transaction.hide(decorateAuditFragment);
        }
    }

    //解决重叠，方法1
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认的tab
        super.onSaveInstanceState(outState);
    }

}
